/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.ColorM;
import MsSQL.ConMsSQL;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Maryluz
 */
public class ColorC {

    public int agregarColor(ColorM oColorM) {

        Connection cnn = ConMsSQL.obtenerConexion();
        try {
            if (cnn != null) {
                String sql = "{call sp_addColor(?,?)}";
                CallableStatement cs = cnn.prepareCall(sql);
                cs.setString(1, oColorM.getCodigo());
                cs.setString(2, oColorM.getNombre());
                ResultSet rs = cs.executeQuery();
                if (rs != null) {
                    while (rs.next()) {
                        return rs.getInt(1);
                    }
                }
                return -1;
            }
        } catch (Exception e) {
            //  System.out.print(e);
            return -1;
        }
        return -1;
    }

    public List<ColorM> listarColores() {
        Connection conexion = ConMsSQL.obtenerConexion();
        if (conexion != null) {
            try {
                List<ColorM> vctColor = new ArrayList<>();
                String sql = "select codigo,nombre from TBL_COLOR where std<>1";

                Statement instruccionSQL = conexion.createStatement();
                ResultSet rs = instruccionSQL.executeQuery(sql);

                if (rs != null) {
                    while (rs.next()) {
                        ColorM oM = new ColorM();
                        oM.setCodigo(rs.getString(1));
                        oM.setNombre(rs.getString(2));
                        vctColor.add(oM);
                    }
                    return vctColor;
                }
                return null;
            } catch (SQLException e) {
                // System.out.print(e);
                return null;
            } catch (Exception e) {
                //System.out.print(e);
                return null;
            } finally {
                try {
                    conexion.close();
                } catch (SQLException ex) {
                    System.out.print("No se puede cerrar la conexion");
                }
            }
        }
        return null;
    }

    @SuppressWarnings("CallToThreadDumpStack")
    public boolean actualizarColor(String nombre, String codigo) {

        Connection cnn = ConMsSQL.obtenerConexion();
        try {
            if (cnn != null) {
                String sql = "update TBL_COLOR set nombre='" + nombre + "' where codigo='" + codigo + "'";
                Statement instruccionSQL = cnn.createStatement();
                instruccionSQL.executeQuery(sql);
                return true;

            }
        } catch (SQLException e) {
            // e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
    
        @SuppressWarnings("CallToThreadDumpStack")
    public boolean borrarColor(String codigo) {

        Connection cnn = ConMsSQL.obtenerConexion();
        try {
            if (cnn != null) {
                String sql = "update TBL_COLOR set std=1 where codigo='" + codigo + "'";
                Statement instruccionSQL = cnn.createStatement();
                instruccionSQL.executeQuery(sql);
                return true;

            }
        } catch (SQLException e) {
            // e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
